import com.cyy.PiJob;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.livy.LivyClient;
import org.apache.livy.LivyClientBuilder;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.ExecutionException;

public class Main {

    public static void main(String[] args) throws IOException, URISyntaxException {


//        Configuration conf = new Configuration();
//        conf.addResource(new Path("/etc/hadoop/2.6.2.0-205/0/core-site.xml"));
//        conf.addResource(new Path("/etc/hadoop/2.6.2.0-205/0/hdfs-site.xml"));
//        System.setProperty("java.security.krb5.conf", "/etc/krb5.conf");
//        UserGroupInformation.setConfiguration(conf);
//        UserGroupInformation.loginUserFromKeytab("nifi@TRENDY-GLOBAL.COM", "/etc/security/keytabs/nifi.headless.keytab");
//        UserGroupInformation.loginUserFromKeytab("nifi@TRENDY-GLOBAL.COM", "file:///C:/Users/yiyang/Desktop/nifi.headless.keytab");



//        String livyUrl= "http://10.2.29.105:8999";
        String livyUrl= "http://139.199.7.37:8998";

        LivyClient client = new LivyClientBuilder()
                .setURI(new URI(livyUrl))
//                .setConf("spnego.enable", "true")
//                .setConf("krb5.debug", "true")
//                .setConf("auth.login.config", "C:\\Users\\yiyang\\Desktop\\login-yarn.conf")
//                .setConf("krb5.conf", "C:\\Users\\yiyang\\Desktop\\krb5.conf")
                .build();
        System.out.print("finish");
        firstPiJob(client);

    }

    private static void firstPiJob(LivyClient client){
        try {
            URI piJar=new URI("file:///E:/cyy/git/ApacheLivyDemo/target/ApacheLivyDemo-1.0.jar");
            System.err.printf("Uploading %s to the Spark context...\n", piJar);
            client.uploadJar(new File(piJar)).get();

            int samples=3;
            System.err.printf("Running PiJob with %d samples...\n", samples);
            double pi = client.submit(new PiJob(samples)).get();

            System.out.println("Pi is roughly: " + pi);
        } catch (InterruptedException | ExecutionException | URISyntaxException e) {
            e.printStackTrace();
        } finally {
            client.stop(true);
        }
    }

}
